VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CBillInItems"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Collection" ,"CBillInItem"
Attribute VB_Ext_KEY = "Member0" ,"CBillInItem"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

'local variable to hold collection
Private mCol As Collection

Implements Ibill

Public Function Add(Money As Double, Quantity As Double, Price As Double, UnitID As Long, ProcessNO As String, PackageID As Long, SpecID As Long, ProductID As Long, OrderNum As Integer, BillID As Long, Optional sKey As String) As CBillInItem
    'create a new object
    Dim objNewMember As CBillInItem
    Set objNewMember = New CBillInItem


    'set the properties passed into the method
    objNewMember.Money = Money
    objNewMember.Quantity = Quantity
    objNewMember.Price = Price
    objNewMember.UnitID = UnitID
    objNewMember.PackageID = PackageID
    objNewMember.SpecID = SpecID
    objNewMember.ProcessNO = ProcessNO
    objNewMember.ProductID = ProductID
    objNewMember.OrderNum = OrderNum
    objNewMember.BillID = BillID
    If Len(sKey) = 0 Then
        mCol.Add objNewMember
    Else
        mCol.Add objNewMember, sKey
    End If


    'return the object created
    Set Add = objNewMember
    Set objNewMember = Nothing


End Function

Public Property Get Item(vntIndexKey As Variant) As CBillInItem
Attribute Item.VB_UserMemId = 0
    'used when referencing an element in the collection
    'vntIndexKey contains either the Index or Key to the collection,
    'this is why it is declared as a Variant
    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
  Set Item = mCol(vntIndexKey)
End Property



Public Property Get Count() As Long
    'used when retrieving the number of elements in the
    'collection. Syntax: Debug.Print x.Count
    Count = mCol.Count
End Property


Public Sub Remove(vntIndexKey As Variant)
    'used when removing an element from the collection
    'vntIndexKey contains either the Index or Key, which is why
    'it is declared as a Variant
    'Syntax: x.Remove(xyz)


    mCol.Remove vntIndexKey
End Sub


Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property


Private Sub Class_Initialize()
    'creates the collection when this class is created
    Set mCol = New Collection
End Sub


Private Sub Class_Terminate()
    'destroys collection when this class is terminated
    Set mCol = Nothing
End Sub

Public Function Load(ByVal BillID As Long) As Long
    Load = Ibill_Load(BillID)
End Function
Public Function LoadDefault(ByVal ContractID As Long, ByVal ApplyBillID As Long) As Long
Dim SQL As String
Dim Rst As ADODB.Recordset
Dim lrtn As Long
Dim msErrorSource As String
Dim Quantity As Double
Dim Money    As Double

    msErrorSource = sprintf("CBillInItems.LoadDefault(),ContractID=%d,ApplyBillID=%d", ContractID, ApplyBillID)

    If ApplyBillID <> gnNULLID Then
        SQL = sprintf("Select OrderNum,ProductID,SpecID,PackageID,ProcessNO,UnitID,Price,Quantity,PayTaxMoney+ValueAddedTax as Money From D_ProductDutyDTL where ApplyID=%d Order by OrderNum", ApplyBillID)
    Else
        SQL = sprintf("Select OrderNum,ProductID,SpecID,PackageID,ProcessNO,UnitID,Price,Quantity,Money From D_ContractDetail Where ContractID =%d Order by OrderNum", ContractID)
    End If
    
    Set Rst = UI.Svr.cntMain.Execute(SQL, , adCmdText)
    Set mCol = New Collection
        
    Do While Not Rst.EOF
        With Rst
            Call Add(.Fields("Money").Value, .Fields("Quantity").Value, .Fields("Price").Value, .Fields("UnitID").Value, CStr(.Fields("ProcessNO").Value), _
                 .Fields("PackageID").Value, .Fields("SpecID").Value, .Fields("ProductID").Value, _
                 .Fields("OrderNum").Value, gnNULLID)
        End With
        Rst.MoveNext
    Loop
 
    
    lrtn = gnSUCCESS
ExitEntry:
    LoadDefault = lrtn
    Exit Function
ErrorAttach:
    lrtn = Err.Number
    UI.Svr.Err.Push lrtn, msErrorSource, Err.Description
    Resume ExitEntry




End Function
Public Function Save(ByVal BillID As Long) As Long
    Save = Ibill_Save(BillID)
End Function

Public Function Check(ByVal WarehouseID, ByVal Period As String, ByVal BillID As Long) As Long
    Check = Ibill_Check(WarehouseID, Period, BillID)
End Function

Private Function Ibill_Load(ByVal BillID As Long) As Long
Dim SQL As String
Dim Rst As ADODB.Recordset
Dim lrtn As Long
Dim msErrorSource As String

    msErrorSource = sprintf("CBillInItems.Load(),BillID=%d", BillID)

    SQL = sprintf("Select * From D_StorageInDetail Where BillID =%d Order by OrderNum", BillID)
    Set Rst = UI.Svr.cntMain.Execute(SQL, , adCmdText)
    Set mCol = New Collection
    
    Do While Not Rst.EOF
        With Rst
            Call Add(.Fields("Money").Value, .Fields("Quantity").Value, .Fields("Price").Value, .Fields("UnitID").Value, CStr(.Fields("ProcessNO").Value), _
                 .Fields("PackageID").Value, .Fields("SpecID").Value, .Fields("ProductID").Value, _
                 .Fields("OrderNum").Value, .Fields("BillID").Value)
        End With
        Rst.MoveNext
    Loop
    
    
    lrtn = gnSUCCESS
ExitEntry:
    Ibill_Load = lrtn
    Exit Function
ErrorAttach:
    lrtn = Err.Number
    UI.Svr.Err.Push lrtn, msErrorSource, Err.Description
    Resume ExitEntry
End Function

Private Function Ibill_Reverse(ByVal BillID As Long) As Long

End Function

Private Function Ibill_Save(ByVal BillID As Long) As Long
On Error GoTo ErrorAttach
Dim CmdBillInItem As ADODB.Command
Dim SQL As String
Dim tmpDetailItem As CBillInItem
Dim RecordsAffected As Integer
Dim lrtn As Long
Dim msErrorSource As String

    msErrorSource = sprintf("CBillInItems.Save()")
    Set CmdBillInItem = New ADODB.Command
    CmdBillInItem.CommandType = adCmdText
    Set CmdBillInItem.ActiveConnection = UI.Svr.cntMain
 
    UI.Svr.cntMain.Execute "Delete From D_StorageInDetail where BillID=" & BillID, RecordsAffected
   
    For Each tmpDetailItem In mCol
        If tmpDetailItem.ProductID <> gnNULLID Then
            With tmpDetailItem
                    SQL = sprintf("Insert Into D_StorageInDetail (BillID,OrderNum,ProductID,SpecID,PackageID,ProcessNo,UnitID,Price,Quantity,Money) Values(%d,%d,%d,%d,%d,'%s',%d,%d,%d,%d)", _
                            .BillID, .OrderNum, .ProductID, .SpecID, .PackageID, .ProcessNO, .UnitID, .Price, .Quantity, .Money)
            End With
            Debug.Print SQL
            CmdBillInItem.CommandText = SQL
            CmdBillInItem.Execute
        End If
    Next
    lrtn = gnSUCCESS
ExitEntry:
    Ibill_Save = lrtn
    Exit Function
ErrorAttach:
    lrtn = Err.Number
    UI.Svr.Err.Push lrtn, msErrorSource, Err.Description
    Resume ExitEntry
End Function

Private Function Ibill_Check(ByVal WarehouseID As Variant, ByVal Period As String, ByVal BillID As Long) As Long
On Error GoTo ErrorAttach
Dim CmdBillInItem As ADODB.Command
Dim SQL As String
Dim tmpDetailItem As CBillInItem
Dim RecordsAffected As Integer
Dim lrtn As Long
Dim msErrorSource As String
Dim TotalMoney As Double

    msErrorSource = sprintf("CBillInItems.Checked(BillID=%d)", BillID)
    Set CmdBillInItem = New ADODB.Command
    CmdBillInItem.CommandType = adCmdText
    Set CmdBillInItem.ActiveConnection = UI.Svr.cntMain
    
    Call Load(BillID)
    
    For Each tmpDetailItem In mCol
         
        With tmpDetailItem
                TotalMoney = .Money
                SQL = sprintf("Insert Into D_StorageData (WarehouseID,Period,BillType,BillID,ProductID,SpecID,PackageID,UnitID,ProcessNo,Price,Quantity,Money) Values(%d,'%s',%d,%d,%d,%d,%d,%d,'%s',%d,%d,%d)", _
                                WarehouseID, Period, gnEnum_Bill_StorageIn, .BillID, .ProductID, .SpecID, .PackageID, .UnitID, .ProcessNO, .Price, .Quantity, TotalMoney)
        End With
        Debug.Print SQL
        CmdBillInItem.CommandText = SQL
        CmdBillInItem.Execute
  
    Next
        
    lrtn = gnSUCCESS
ExitEntry:
    Ibill_Check = lrtn
    Exit Function
ErrorAttach:
    lrtn = Err.Number
    UI.Svr.Err.Push lrtn, msErrorSource, Err.Description
    Resume ExitEntry
End Function
